const { db, query } = require("../../db/db");
const util = require("util");
const moment = require("moment");

function CreateArticle(req, res) {
  console.log("输入内容", req.body, req.params);

  let ArticleTitle = req.body.ArticleTitle;
  let Abstract = req.body.Abstract;
  let ArticleContent = req.body.ArticleContent;
  let ArticleImgUrl = req.body.ArticleImgUrl;
  let ArticleTabs = req.body.ArticleTabs.join(",");
  let ArticleType = req.body.ArticleType;
  let PublisherId = Number(req.params.username);
  let PublishTime = moment().format("YYYY-MM-DD HH:mm:ss");

  const insert_article =
    "Insert INTO Article \
  (ArticleTitle,Abstract,ArticleContent,PublisherId,PublishTime,ArticleImgUrl,ArticleTabs,ArticleType) \
  values(?,?,?,?,?,?,?,?)";

  db.query(
    insert_article,
    [
      ArticleTitle,
      Abstract,
      ArticleContent,
      PublisherId,
      PublishTime,
      ArticleImgUrl,
      ArticleTabs,
      ArticleType,
    ],
    (err, results) => {
      if (err) {
        res.send({
          Message: err,
          status: 1,
        });
      }

      res.send({
        status: 0,
      });
    }
  );
}

module.exports = { CreateArticle };
